package com.six666.medicaldevicetestingsystem.service.impl;

import com.six666.medicaldevicetestingsystem.dto.Equipment_Order_DTO;
import com.six666.medicaldevicetestingsystem.dto.Equipment_Scheme_DTO;
import com.six666.medicaldevicetestingsystem.dto.SampleList_number_DTO;
import com.six666.medicaldevicetestingsystem.dto.Sample_Order_DTO;
import com.six666.medicaldevicetestingsystem.entity.MessageList;
import com.six666.medicaldevicetestingsystem.entity.Project;
import com.six666.medicaldevicetestingsystem.entity.ProjectTest;
import com.six666.medicaldevicetestingsystem.entity.UserAccount;
import com.six666.medicaldevicetestingsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class Export_Service_Impl implements Export_Service {
    @Autowired
    AccountOrder_Service accountorder_service;
    @Autowired
    ProjectOfficers_Service projectofficers_service;
    @Autowired
    Project_Service project_service;
    @Autowired
    Equipment_Service equipment_service;
    @Autowired
    MessageList_Service message_list_service;
    @Autowired
    Account_Service account_service;
    @Autowired
    ProjectTest_Service project_test_service;
    @Autowired
    TestOffiers_Service testoffiers_service;

    @Override
    public List<Map<String, String>> export_equip_order() {
        List<Map<String, String>> res = new ArrayList<>();
        List<Equipment_Order_DTO>all_order= accountorder_service.get_all_equip_order();
        for (Equipment_Order_DTO order : all_order) {
            for (Equipment_Scheme_DTO scheme : order.getDetail()) {
                int project_id=equipment_service.get_project_id_by_equipment_id(scheme.getEquipment_id());
                Project project = project_service.get_by_id(project_id);
                Map<String, String> temp = new HashMap<>();
                temp.put("计划采购人", order.getUser().getName());
                temp.put("类别(产品/项目/参数)", project.getProjecttype());
                temp.put("产品/项目/参数:名称",project.getProjectname());
                temp.put("标准名称",project.getProjecttype());
                temp.put("标准编号（含年号）及条款号",project.getStandardnumber());
                temp.put("计划采购设备",scheme.getScheme_name()+"x"+scheme.getNum());
                if(order.getOrder_state().equals("Finish")){
                    temp.put("状态（计划采购、采购完成）", "采购完成");
                }
                if(order.getOrder_state().equals("Ongoing")){
                    temp.put("状态（计划采购、采购完成）", "计划采购");
                }
                res.add(temp);
            }
        }
        return res;
    }

    @Override
    public String[] get_equip_head(){
        String[] res = new String[7];
        res[0]="计划采购人";
        res[1]="类别(产品/项目/参数)";
        res[2]="产品/项目/参数:名称";
        res[3]="标准名称";
        res[4]="标准编号（含年号）及条款号";
        res[5]="计划采购设备";
        res[6]="状态（计划采购、采购完成）";
        return res;
    }

    @Override
    public String[] get_people_order() {
        String[] res = new String[7];
        res[0]="人员";
        res[1]="类别(产品/项目/参数)";
        res[2]="产品/项目/参数:名称";
        res[3]="标准名称";
        res[4]="标准编号（含年号）及条款号";
        res[5]="培训完成时间";
        res[6]="考核完成时间";
        return res;
    }

    @Override
    public List<Map<String, String>> export_people_order() {
        return projectofficers_service.get_excel();
    }

    @Override
    public List<Map<String, String>> export_sample_order() {
        List<Map<String, String>> res = new ArrayList<>();
        List<Sample_Order_DTO> all_order= accountorder_service.get_all_sample_order();
        for (Sample_Order_DTO order : all_order) {
            for (SampleList_number_DTO sample : order.getDetail()) {
                int project_id=sample.getProjectid();
                Project project = project_service.get_by_id(project_id);
                Map<String, String> temp = new HashMap<>();
                temp.put("需求人", order.getUser().getName());
                temp.put("标准名称", project.getProjecttype());
                temp.put("标准编号（含年号）及条款号",project.getStandardnumber());
                temp.put("产品名称",project.getProjecttype());
                temp.put("规格型号",sample.getModel());
                temp.put("申请需求数量", String.valueOf(sample.getNumber()));
                temp.put("样品名称", String.valueOf(sample.getSamplename()));
                temp.put("生产厂家", sample.getSource());
                String strDate="无";
                if (order.getOrder_time()!=null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    strDate = sdf.format(order.getOrder_time());
                }
                temp.put("申请需求时间", strDate);
                if (order.getOrder_state().equals("Finish")) {
                    temp.put("是否已发放", "是");
                }
                else {
                    temp.put("是否已发放", "否");
                }
                res.add(temp);
            }
        }
        return res;
    }

    @Override
    public String[] get_sample_head() {
        String[] res = new String[10];
        res[0]="需求人";
        res[1]="标准名称";
        res[2]="标准编号（含年号）及条款号";
        res[3]="产品名称";
        res[4]="规格型号";
        res[5]="申请需求数量";
        res[6]="样品名称";
        res[7]="生产厂家";
        res[8]="申请需求时间";
        res[9]="是否已发放";
        return res;
    }

    @Override
    public List<Map<String, String>> export_message_list() {
        List<Map<String, String>> res = new ArrayList<>();
        List<MessageList> messages= message_list_service.get_all_message();
        for (MessageList message : messages) {
            int project_id=message.getProjectid();
            Project project = project_service.get_by_id(project_id);
            UserAccount user=account_service.get_by_id(message.getAccountid());
            Map<String, String> temp = new HashMap<>();
            temp.put("申请人", user.getName());
            temp.put("申请时间", project.getProjecttype());
            temp.put("标准名称",project.getStandardnumber());
            temp.put("标准编号（含年号）及条款号",project.getStandardnumber());
            temp.put("项目",project.getProjectname());
            switch (message.getMessagetype()) {
                case "Offiers" -> temp.put("类型", "人员");
                case "Equipment" -> temp.put("类型", "设备");
                case "Sample" -> temp.put("类型", "样品");
                default -> temp.put("类型", "规程");
            }
            temp.put("内容", String.valueOf(message.getMessage()));
            temp.put("是否已解决", message.getState().equals("Finish")?"是":"否");
            res.add(temp);
        }
        return res;
    }

    @Override
    public String[] get_message_head() {
        String[] res = new String[8];
        res[0]="申请人";
        res[1]="申请时间";
        res[2]="标准名称";
        res[3]="标准编号（含年号）及条款号";
        res[4]="项目";
        res[5]="类型";
        res[6]="内容";
        res[7]="是否已解决";
        return res;
    }

    @Override
    public List<Map<String, String>> export_compare_plan_order() {
        List<Map<String, String>> res = new ArrayList<>();
        List<ProjectTest> projectTests=project_test_service.get_all_test();
        for (ProjectTest projectTest : projectTests) {
            List<Integer> test_ids=new ArrayList<>();
            test_ids.add(projectTest.getTestid());
            List<Integer> account_ids=testoffiers_service.get_account_id_by_test_id(test_ids);
            for(Integer account_id:account_ids){
                Map<String, String> temp = new HashMap<>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = sdf.format(projectTest.getTesttime());
                temp.put("实验时间",date);
                temp.put("状态",projectTest.getState());
                temp.put("比对测试申请人",account_service.get_by_id(account_id).getName());
                Project project=project_service.get_by_id(projectTest.getProjectid());
                temp.put("类别(产品/项目/参数)",project.getProjecttype());
                temp.put("产品/项目/参数:名称",project.getProjectname());
                temp.put("标准名称",project.getProjecttype());
                temp.put("标准编号（含年号）及条款号",project.getStandardnumber());
                res.add(temp);
            }
        }
        return res;
    }

    @Override
    public String[] get_compare_plan_order() {
        String[] res = new String[7];
        res[0]="比对测试申请人";
        res[1]="类别(产品/项目/参数)";
        res[2]="产品/项目/参数:名称";
        res[3]="标准名称";
        res[4]="标准编号（含年号）及条款号";
        res[5]="实验时间";
        res[6]="状态";
        return res;
    }
}
